package cn.trigram.example.designpattern.fsm;


import cn.trigram.example.designpattern.fsm.event.AbstractEvent;
import cn.trigram.example.designpattern.fsm.listener.TransitionListener;
import cn.trigram.example.designpattern.fsm.state.AbstractState;
import java.util.ArrayList;
import java.util.List;

/**
 * 状态机上下文
 */
public class StateContext {

  protected StateMachine stateMachine;

  protected AbstractState currentState;

  protected AbstractEvent currentEvent;

  protected final List<TransitionListener> transitionListeners = new ArrayList<>();

  public StateContext() {

  }

  public StateContext(AbstractState currentState, AbstractEvent currentEvent,
      StateMachine stateMachine) {

    this.currentState = currentState;
    this.currentEvent = currentEvent;
    this.stateMachine = stateMachine;
  }

  public void updateState(AbstractState state) {

    this.currentState = state;
    AbstractEvent handleEvent = this.currentState.handle(this);
    this.stateMachine.submit(handleEvent);
  }

  public StateMachine getStateMachine() {

    return stateMachine;
  }

  public void setStateMachine(StateMachine stateMachine) {

    this.stateMachine = stateMachine;
  }

  public AbstractState getCurrentState() {

    return currentState;
  }

  public void setCurrentState(AbstractState currentState) {

    this.currentState = currentState;
  }

  public AbstractEvent getCurrentEvent() {

    return currentEvent;
  }

  public void setCurrentEvent(AbstractEvent currentEvent) {

    this.currentEvent = currentEvent;
  }

  public List<TransitionListener> getTransitionListeners() {

    return transitionListeners;
  }

}